#!/bin/bash


currentdir=$(cd "$(dirname "$0")"; pwd)
echo "directory: ${currentdir}."

os=$(uname -o)
echo "system: ${os}."

find $currentdir/libcomm -iregex ".*\(\.h\|\.cpp\|\.c\)$" -exec dos2unix {} \;

LIBCOMM_VERSION=$(sed -n '/COMM_VERSION/p' $currentdir/libcomm/Comm.h |  sed -n '1p' | awk '{print $3}' | sed 's/\"//g')
echo "COMLIB version: ${LIBCOMM_VERSION}."

function build_depend()
{

echo -e "\nbuild libevent..."
BUILD_LIBEVENT=$(sed -n '/COMM_FEATURE_LIBEVENT/p' $currentdir/libcomm/Comm.h |  sed -n '1p' | awk '{print $3}')
echo "BUILD_LIBEVENT: $BUILD_LIBEVENT"
if [[ $BUILD_LIBEVENT == 1* ]]; then
	cd $currentdir/depend/libevent/
	#chmod +x ./build.sh
	#chmod +x autogen.sh
	#chmod +x configure
	./build.sh
fi

echo -e "\nbuild pcre..."
BUILD_PCRE=$(sed -n '/COMM_FEATURE_REGEX/p' $currentdir/libcomm/Comm.h |  sed -n '1p' | awk '{print $3}')
echo "BUILD_PCRE: $BUILD_PCRE"
if [[ $BUILD_PCRE == 1* ]]; then
	cd $currentdir/depend/pcre/
	#chmod +x ./build.sh
	#chmod +x configure
	./build.sh
fi

return 0
}

function build_libcomm()
{
echo -e "\nbuild libcomm..."
cd $currentdir/libcomm

NEED_SSL=$(sed -n '/COMM_FEATURE_SSL/p' $currentdir/libcomm/Comm.h |  sed -n '1p' | awk '{print $3}')
if [[ $NEED_SSL == 1* ]]; then
	echo "NEED SSL..."	
	sed -i 's/^LIBRARY_NAMES.*/LIBRARY_NAMES := pthread dl ssl/g' $currentdir/libcomm/debug.mak
	sed -i 's@^ADDITIONAL_LINKER_INPUTS.*@ADDITIONAL_LINKER_INPUTS := ../../lib/libpcre.a ../../lib/libpcreposix.a ../../lib/libevent_core.a ../../lib/libevent_pthreads.a ../../lib/libevent_extra.a ../../lib/libevent_openssl.a@g' $currentdir/libcomm/debug.mak
else
	echo "NOT NEED SSL..."
	sed -i 's/^LIBRARY_NAMES.*/LIBRARY_NAMES := pthread dl/g' $currentdir/libcomm/debug.mak
	sed -i 's@^ADDITIONAL_LINKER_INPUTS.*@ADDITIONAL_LINKER_INPUTS := ../../lib/libpcre.a ../../lib/libpcreposix.a ../../lib/libevent_core.a ../../lib/libevent_pthreads.a ../../lib/libevent_extra.a@g' $currentdir/libcomm/debug.mak

fi


if [[ ${os} == Cygwin || ${os} == Msys ]]; then
	sed -i 's/^PREPROCESSOR_MACROS.*/PREPROCESSOR_MACROS := DEBUG=1 _GNU_SOURCE/g' $currentdir/libcomm/debug.mak
	
	if [[ $NEED_SSL == 1* ]]; then
		sed -i 's/^LIBRARY_NAMES.*/LIBRARY_NAMES := pthread dl ssl iconv/g' $currentdir/libcomm/debug.mak
	else
		sed -i 's/^LIBRARY_NAMES.*/LIBRARY_NAMES := pthread dl iconv/g' $currentdir/libcomm/debug.mak
	fi
	sed -i 's/^LDFLAGS.*/LDFLAGS := -Wl,-gc-sections  -Wl,--out-implib=\.\/Debug\/libcomm.dll.a/g' $currentdir/libcomm/debug.mak
	sed -i 's/^TARGETNAME.*/TARGETNAME := libcomm.dll/g' $currentdir/libcomm/Makefile
fi

make clean
make -j 4 CONFIG=Debug


if [[ ${os} == Cygwin || ${os} == Msys ]]; then
	if [ ! -f "$currentdir/libcomm/Debug/libcomm.dll" ]; then return 1; fi
	cp -v $currentdir/libcomm/Debug/libcomm.dll $currentdir/../bin/
	cp -v $currentdir/libcomm/Debug/libcomm.dll.a $currentdir/../bin/
else
	if [ ! -f "$currentdir/libcomm/Debug/libcomm.so" ]; then return 1; fi
	cp -v $currentdir/libcomm/Debug/libcomm.so $currentdir/../bin/
fi
make clean
return 0
}

function build_test()
{
echo -e "\nbuild test..."
cd $currentdir/test
make clean
make CONFIG=Debug
if [ ! -f "$currentdir/test/Debug/test" ]; then return 1; fi
cp -v $currentdir/test/Debug/test $currentdir/../bin/
make clean
return 0
}

function setup()
{
setupdir="/tmp/libcomm"
if [ -d "${setupdir}" ]; then rm -rf ${setupdir}; fi
if [ ! -d "${setupdir}/include" ]; then mkdir -p "${setupdir}/include"; fi
cp -v $currentdir/libcomm/*.h  $setupdir/include
rm -rf $setupdir/include/gcc*.h
if [ ! -d "${setupdir}/bin" ]; then mkdir -p "${setupdir}/bin"; fi
cp -v $currentdir/../bin/*  $setupdir/bin
cd $setupdir
date=`date +%Y%m%d%H%M`
platform=`uname -m`
tar czf libcomm-devel_${platform}_${LIBCOMM_VERSION}_${date}.tar.gz *
mv libcomm-devel_${platform}_${LIBCOMM_VERSION}_${date}.tar.gz $currentdir/
}


build_depend
if [ $? -ne 0 ]; then exit 1; fi
build_libcomm
if [ $? -ne 0 ]; then exit 1; fi
#build_test
#if [ $? -ne 0 ]; then exit 1; fi
setup

echo "All build success !"
